package com.tju.resource.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tju.resource.mapper.TbConsumablesMapper;
import com.tju.resource.mapper.TbSupplierMapper;
import com.tju.resource.model.TbConsumables;
import com.tju.resource.model.TbSupplier;
import com.tju.resource.server.TbOutwardService;
import com.tju.resource.server.TbSupplierService;
import com.tju.resource.tool.result.Result;
import com.tju.resource.tool.result.ResultEnum;
import com.tju.resource.tool.result.ResultUtil;
import com.tju.resource.tool.tool.ClientParameters;
import com.tju.resource.tool.tool.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TbSupplierServiceImpl implements TbSupplierService {
    @Autowired
    TbSupplierMapper tbSupplierMapper;
    @Autowired
    TbConsumablesMapper tbConsumablesMapper;
    @Autowired
    TbOutwardService tbOutwardService;

    /**
     * 查询
     *
     * @param parameters
     * @return
     */
    @Override
    public Result<Object> getSupplier(ClientParameters parameters) {
        try {
            QueryWrapper<TbSupplier> wrapper = new QueryWrapper<>();
            wrapper.like("supplier_name", parameters.getOne());
            Page<TbSupplier> page = new Page<>(parameters.getPage(), parameters.getLimit());
            IPage<TbSupplier> iPage = tbSupplierMapper.selectPage(page, wrapper);
            return ResultUtil.success(ResultEnum.SUCCESS, iPage.getRecords(), iPage.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR);
    }

    /**
     * 添加
     *
     * @param tbSupplier
     * @return
     */
    @Transactional
    @Override
    public Result<Object> addSupplier(TbSupplier tbSupplier) {
        QueryWrapper<TbSupplier> wrapper = new QueryWrapper<>();
        wrapper.eq("supplier_code", tbSupplier.getSupplierCode()).or();
        wrapper.eq("supplier_name", tbSupplier.getSupplierName());
        if (EmptyUtils.isEmpty(tbSupplierMapper.selectList(wrapper))) {
            if (tbSupplierMapper.insert(tbSupplier) > 0) {
                return ResultUtil.success(ResultEnum.SUCCESS, "添加成功!");
            } else
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "添加失败！");
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, "供应商已存在！");
    }

    /**
     * 删除
     *
     * @param tbSupplier
     * @return
     */
    @Transactional
    @Override
    public Result<Object> delSupplier(TbSupplier tbSupplier) {
        if (tbOutwardService.judgmentSupplier(tbSupplier.getId())) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "当前供应商已在外发管理使用！");
        }
        QueryWrapper<TbConsumables> wrapper = new QueryWrapper<>();
        wrapper.eq("supplier_id", tbSupplier.getId());
        if (EmptyUtils.isNotEmpty(tbConsumablesMapper.selectList(wrapper))) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "当前供应商已在耗材管理使用！");
        }
        if (tbSupplierMapper.deleteById(tbSupplier.getId()) > 0) {
            return ResultUtil.success(ResultEnum.SUCCESS, "删除成功");
        } else
            return ResultUtil.error(ResultEnum.UNKONW_ERROR);
    }

    /**
     * 修改
     *
     * @param tbSupplier
     * @return
     */
    @Transactional
    @Override
    public Result<Object> updSupplier(TbSupplier tbSupplier) {
        QueryWrapper<TbSupplier> wrapper = new QueryWrapper<>();
        wrapper.eq("supplier_code", tbSupplier.getSupplierCode()).or();
        wrapper.eq("supplier_name", tbSupplier.getSupplierName());
        List<TbSupplier> list = tbSupplierMapper.selectList(wrapper);
        if (list.size() == 1) {
            if (!tbSupplier.getId().equals(list.get(0).getId()))
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "供应商已存在！");
        } else if (list.size() == 2)
            if (!tbSupplier.getId().equals(list.get(1).getId()))
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "供应商已存在！");
        if (tbSupplierMapper.updateById(tbSupplier) > 0) {
            return ResultUtil.success(ResultEnum.SUCCESS, "修改成功！");
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, "修改失败！");
    }

}
